package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.entity.Association;
import com.jeff.tianti.cms.service.AssociationService;

import javax.servlet.http.HttpServletRequest;

import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.cms.dto.AssociationQueryDTO;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author NOW_GO
 * @desc AssociationController
 * @date 2019-07-12
 */
@Controller
@RequestMapping("/channel/association")
public class AssociationController {

    @Autowired
    private AssociationService associationService;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String name = request.getParameter("name");
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        AssociationQueryDTO associationQueryDTO = new AssociationQueryDTO();
        associationQueryDTO.setCurrentPage(currentPage);
        associationQueryDTO.setPageSize(pageSize);
        associationQueryDTO.setName(name);
        PageModel<Association> page = this.associationService.queryAssociationPage(associationQueryDTO);
        model.addAttribute("page", page);
        model.addAttribute("associationQueryDTO", associationQueryDTO);
        model.addAttribute(Constants.MENU_NAME, Constants.MENU_ASSOCIATION);


        return "/channel/association/list";
    }

    @RequestMapping("/add")
    public String add(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        Association association = null;
        if (StringUtils.isNotBlank(id)) {
            association = this.associationService.find(id);
        }
        model.addAttribute("association", association);
        model.addAttribute("id", id);
        return "/channel/association/edit";
    }

    @RequestMapping("/saveorupdate")
    @ResponseBody
    public AjaxResult doSaveOrUpdate(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String logo = request.getParameter("logo");
            String name = request.getParameter("name");
            String president = request.getParameter("president");
            String secretaryGeneral = request.getParameter("secretaryGeneral");
            String vicePresident = request.getParameter("vicePresident");
            String address = request.getParameter("address");
            Integer sort = Integer.parseInt(request.getParameter("sort"));
            Association association = null;
            if (StringUtils.isNotBlank(id)) {
                association = this.associationService.find(id);
                association.setUpdateDate(new Date());
                association.setLogo(logo);
                association.setName(name);
                association.setPresident(president);
                association.setSecretaryGeneral(secretaryGeneral);
                association.setVicePresident(vicePresident);
                association.setAddress(address);
                association.setUpdateDate(new Date());
                association.setSort(sort);
                this.associationService.update(association);
            } else {
                association = new Association();
                association.setLogo(logo);
                association.setName(name);
                association.setPresident(president);
                association.setSecretaryGeneral(secretaryGeneral);
                association.setVicePresident(vicePresident);
                association.setAddress(address);
                association.setCreateDate(new Date());
                association.setSort(sort);
                this.associationService.save(association);
            }
            ajaxResult.setSuccess(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult doDelete(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            Association association = this.associationService.find(id);
            if (association != null) {
                association.setDeleteFlag("1");
                this.associationService.update(association);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }


}